//
//  TopViewController.m
//  多玩盒子
//
//  Created by 刘胜 on 16/2/26.
//  Copyright © 2016年 刘胜. All rights reserved.
//

#import "TopViewController.h"
#import "RootTableViewCell.h"
#import "LSZNetWorkTools.h"
#import "TopModel.h"
#import "WebViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define WID [[UIScreen mainScreen] bounds].size.width / 375
#define HEI [[UIScreen mainScreen] bounds].size.height / 667

@interface TopViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView * myTableView;
@property(nonatomic,strong)NSMutableArray * allArray;

@end

@implementation TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    [self creatTableView];
    [self getData];
}

-(void)getData {
    NSString * url = @"http://box.dwstatic.com/apiNewsList.php?action=l&newsTag=headlineNews&p=1&v=166&OSType=iOS9.2.1&versionName=2.5.1 ";

     NSString *encoded = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [LSZNetWorkTools getWithUrl:encoded parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSMutableArray * array = [result objectForKey:@"data"];
                for (NSMutableDictionary * dic in array) {
                    TopModel * model = [TopModel modelWithDictionary:dic];
                    [self.allArray addObject:model];
                }
                [self.myTableView reloadData];

        
    } fail:^(NSError *error) {
        
        
    }];
}

-(void)creatTableView {
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,40, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[RootTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.myTableView];
}

#pragma mark === tableViewDelegat, tableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RootTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.cellModel = [self.allArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WebViewController * webVC = [[WebViewController alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    TopModel * model = [self.allArray objectAtIndex:indexPath.row];
    NSString * url = model.destUrl;
    webVC.mol = url;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return HEIGHT/7;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
